package com.example.helloword.request;

import android.util.Log;


import com.example.helloword.background.EventManager;
import com.example.helloword.background.SourceListener;
import com.example.helloword.source.BaseNovelSource;
import com.example.helloword.source.BiqupaiNoveSource;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.Iterator;
import java.util.List;

public class RequestBookController implements Runnable {
    public final static int BOOK_LIST = 0;
    public final static int CHAPTER_LIST = 1;
    public final static int CONTENT = 2;
    private int data_type;
    private String url;
    private String searchName;

    public RequestBookController(int data_type, String url, String searchName) {
        this.data_type = data_type;
        this.url = url;
        this.searchName = searchName;
    }

    @Override
    public void run() {

        BaseNovelSource biqupaiNoveSource = new BiqupaiNoveSource();
        if (data_type == BOOK_LIST) {
            url = biqupaiNoveSource.getSearchUrl(searchName);
        }


        RequestParams params = new RequestParams(url);


        x.http().get(params, new Callback.CommonCallback<String>() {

            @Override
            public void onSuccess(String result) {

                Log.e("crawl", "Td请求成功");
                switch (data_type) {
                    case BOOK_LIST: {
                        List items = biqupaiNoveSource.getItems(result, "div.result-item.result-game-item", BaseNovelSource.BOOK_LIST);
                        Iterator<SourceListener> iterator = EventManager.getInstance().getSourceListeners().iterator();
                        while (iterator.hasNext()) {
                            iterator.next().getBookList(items);
                        }
                        break;

                    }
                    case CONTENT: {
                        String content = biqupaiNoveSource.getContent(result, "div#content");
                        Iterator<SourceListener> iterator = EventManager.getInstance().getSourceListeners().iterator();
                        while (iterator.hasNext()) {
                            iterator.next().getContent(content);
                        }
                        break;
                    }
                    case CHAPTER_LIST: {
                        List items = biqupaiNoveSource.getItems(result, "dd a", BaseNovelSource.CHAPTER_LIST);
                        Iterator<SourceListener> iterator = EventManager.getInstance().getSourceListeners().iterator();
                        while (iterator.hasNext()) {
                            iterator.next().getChapterList(items);
                        }

                        break;
                    }
                }


            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.e("crawl", "Td请求失败");
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
            }
        });


    }
}
